﻿///单选框控件
CKEDITOR.dialog.add('wfradio', function (editor) {
    var controlPre = 'bwdf_rbl_';

    return {
        title: '多选框',
        minWidth: 350,
        minHeight: 210,
        buttons: [CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton],
        onShow: function (element) {
            delete this.wfradio;
            var element = this.getParentEditor().getSelection().getSelectedElement();
            if (element && element.getName() == "input" && element.getAttribute('type') == "radio") {
                this.wfradio = element.getParent();
                this.setupContent(element.getParent());
            }
            else if (element && element.getName() == "div" && element.getAttribute('itemtype') == "5") {
                this.wfradio = element;
                this.setupContent(element);
            }
        },
        onOk: function (element) {
            var editor,
				element = this.wfradio,
				isInsertMode = !element;

            if (isInsertMode) {
                editor = this.getParentEditor();
                element = new CKEDITOR.dom.element('div');
                element.setAttribute('itemtype', '5');
                editor.insertElement(element);
            }
            this.commitContent(element);
        },
        contents: [
			{
			    id: 'BaseSettings',
			    label: "基本设置",
			    elements: [
                    {
                        id: 'name',
                        type: 'text',
                        label: "字段名称",
                        labelLayout: 'horizontal',
                        labelStyle: 'width:50px;',
                        inputStyle: 'width:250px;',
                        'default': '',
                        accessKey: 'N',
                        setup: function (element) {
                            if (element.getAttribute('name')) {
                                this.setValue(element.getAttribute('name').replace(controlPre, '') || '');
                            }
                        },
                        commit: function (element) {
                            if (this.getValue() && (checkName(this.getValue()) || element.getAttribute('name') == controlPre + this.getValue())) {
                                element.setAttribute('id', controlPre + this.getValue());
                                element.setAttribute('name', controlPre + this.getValue());
                            }
                            else {
                                if (!element.getAttribute('name')) {
                                    element.remove();
                                }
                                alert("字段名称不能重复和不能空！！！");
                                throw "字段名称不能重复和不能空";
                            }
                        }
                    },
					{
					    id: 'fname',
					    type: 'text',
					    label: "字段意义",
					    labelLayout: 'horizontal',
					    labelStyle: 'width:50px;',
					    inputStyle: 'width:250px;',
					    setup: function (element) {
					        this.setValue(element.getAttribute('fname') || '');
					    },
					    commit: function (element) {
					        element.setAttribute('fname', this.getValue() || '');
					    }
					},
                    {
                        id: 'comment',
                        type: 'text',
                        label: '字段注释',
                        labelStyle: 'width:50px;',
                        inputStyle: 'width:250px;',
                        labelLayout: 'horizontal',
                        setup: function (element) {
                            this.setValue(element.getAttribute('comment') || '');
                        },
                        commit: function (element) {
                            element.setAttribute('comment', this.getValue() || '');
                        }
                    },
					{
					    id: 'defaultvalue',
					    type: 'text',
					    label: '默认值',
					    labelStyle: 'width:50px;',
					    inputStyle: 'width:250px;',
					    labelLayout: 'horizontal',
					    setup: function (element) {
					        this.setValue(element.getAttribute('defaultvalue') || '');
					    },
					    commit: function (element) {
					        element.setAttribute('defaultvalue', this.getValue() || '');
					    }
					}
					,
                    {
                        id: 'itemdatatype',
                        type: 'select',
                        label: '数据类型',
                        labelStyle: 'width:50px;',
                        inputStyle: 'width:250px;',
                        labelLayout: 'horizontal',
                        'default': 'String',
                        items: ItemDataType.RadioButtonList,
                        setup: function (element) {
                            this.setValue(element.getAttribute('itemdatatype') || '');
                        },
                        commit: function (element) {
                            element.setAttribute('itemdatatype', this.getValue() || '');
                        }
                    },
                    {
                        id: 'datasource',
                        type: 'text',
                        label: '选项内容',
                        labelStyle: 'width:50px;',
                        inputStyle: 'width:250px;',
                        labelLayout: 'horizontal',
                        setup: function (element) {
                            this.setValue(element.getAttribute('datasource') || '');
                        },
                        commit: function (element) {
                            element.setAttribute('datasource', this.getValue() || '');
                            var radios = this.getValue().split(','),
                                defaultvalue = this.getDialog().getContentElement('BaseSettings', 'defaultvalue').getValue().split(','),
                                requirevalue = this.getDialog().getContentElement('ValidateSettings', 'require').getValue(),
                                commentvalue = this.getDialog().getContentElement('BaseSettings', 'comment').getValue(),
                                namevalue = this.getDialog().getContentElement('BaseSettings', 'name').getValue();
                            element.setHtml('');
                            for (var i = 0; i < radios.length; i++) {
                                radio = new CKEDITOR.dom.element('input');
                                radio.setAttribute('type', 'radio');
                                radio.setAttribute('value', radios[i]);
                                radio.setAttribute('id', controlPre + namevalue + '_' + i);
                                radio.setAttribute('name', controlPre + namevalue);
                                radio.setAttribute('itemtype', '5');
                                for (var j in defaultvalue) {
                                    if (radios[i] == defaultvalue[j]) {
                                        radio.setAttribute('checked', 'checked');
                                    }
                                }
                                element.append(radio);
                                span = new CKEDITOR.dom.element('span');
                                span.setText(radios[i]);
                                element.append(span);
                            }
                            if (requirevalue) {
                                var span = new CKEDITOR.dom.element('span');
                                span.setAttribute('class', 'require');
                                span.setHtml("*");
                                element.append(span);
                            }
                            if (commentvalue) {
                                var span = new CKEDITOR.dom.element('span');
                                span.setAttribute('class', 'comment');
                                span.setHtml('(' + commentvalue + ')');
                                element.append(span);
                            }
                        }
                    },
					{
					    type: 'html',
					    html: '<span style="color:blue;">单选框选项内容,请使用英文逗号","分隔每项<span>'
					}
				]
			},
            {
                id: 'ValidateSettings',
                label: "字段验证",
                elements: [
                    {
                        type: 'hbox',
                        widths: ['30%', '70%'],
                        children: [
                            {
                                type: 'html',
                                html: '是否允许为空'
                            },
                            {
                                id: 'require',
                                type: 'checkbox',
                                label: "不允许",
                                'default': false,
                                setup: function (element) {
                                    this.setValue(element.getAttribute('require') || '');
                                },
                                commit: function (element) {
                                    element.setAttribute('require', this.getValue() || '');
                                }
                            }
                        ]
                    },
					{
					    id: 'validatetype',
					    type: 'select',
					    label: '验证类型',
					    labelStyle: 'width:50px;',
					    inputStyle: 'width:250px;',
					    labelLayout: 'horizontal',
					    'default': '',
					    items: ValidateType.RadioButtonList,
					    setup: function (element) {
					        this.setValue(element.getAttribute('validatetype') || '');
					    },
					    commit: function (element) {
					        element.setAttribute('validatetype', this.getValue() || '');
					    }
					},
                    {
                        id: 'msg',
                        type: 'text',
                        label: "错误信息",
                        labelStyle: 'width:50px;',
                        inputStyle: 'width:250px;',
                        labelLayout: 'horizontal',
                        setup: function (element) {
                            this.setValue(element.getAttribute('msg') || '');
                        },
                        commit: function (element) {
                            element.setAttribute('msg', this.getValue() || '');
                        }
                    }
                ]
            },
            {
                id: ' AdvancedSettings',
                label: "高级设置",
                title: editor.lang.textfield.title,
                elements: [
                    {
                        type: 'hbox',
                        widths: ['50%', '50%'],
                        children:
						[
					        {
					            id: 'showset',
					            type: 'select',
					            label: '不显示',
					            size: 10,
					            multiple: true,
					            style: 'width:160px',
					            items: step,
					            setup: function (element) {
					                var showset = element.getAttribute('showset').split(','),
                                        select = this.getInputElement(),
					                    options = this.getInputElement().getChildren();

					                for (var i = 0; i < options.count(); i++) {
					                    for (var j = 0; j < showset.length; j++) {
					                        if (select.$.options[i].value == showset[j]) {
					                            select.$.options[i].selected = true;
					                        }
					                    }
					                }
					                if (showset.length == 0) {
					                    this.setValue('');
					                }
					                else {
					                    select.$.options[0].selected = false;
					                }
					            },
					            commit: function (element) {
					                var options = this.getInputElement().getChildren(),
                                        select = this.getInputElement(),
					                    selectValue = "";

					                for (var i = 0; i < options.count(); i++) {
					                    if (select.$.options[i].selected && select.$.options[i].value != '') {
					                        selectValue += select.$.options[i].value + ",";
					                    }
					                }
					                selectValue = selectValue.substr(0, selectValue.length - 1);
					                element.setAttribute('showset', selectValue);
					            }
					        },
                            {
                                id: 'readonlyset',
                                type: 'select',
                                label: '可编辑',
                                size: 10,
                                multiple: true,
                                style: 'width:160px',
                                items: step,
                                setup: function (element) {
                                    var showset = element.getAttribute('readonlyset').split(','),
                                        select = this.getInputElement(),
					                    options = this.getInputElement().getChildren();

                                    for (var i = 0; i < options.count(); i++) {
                                        for (var j = 0; j < showset.length; j++) {
                                            if (select.$.options[i].value == showset[j]) {
                                                select.$.options[i].selected = true;
                                            }
                                        }
                                    }
                                    if (showset.length == 0) {
                                        this.setValue('');
                                    }
                                    else {
                                        select.$.options[0].selected = false;
                                    }
                                },
                                commit: function (element) {
                                    var options = this.getInputElement().getChildren(),
                                        select = this.getInputElement(),
					                    selectValue = "";

                                    for (var i = 0; i < options.count(); i++) {
                                        if (select.$.options[i].selected && select.$.options[i].value != '') {
                                            selectValue += select.$.options[i].value + ",";
                                        }
                                    }
                                    selectValue = selectValue.substr(0, selectValue.length - 1);
                                    element.setAttribute('readonlyset', selectValue);
                                }
                            }
                        ]
                    }
                ]
            }
		]
    };
});